import {
	ACCESS_TOKEN,
	USER_ID
} from '@/store/mutation-types'
import storage from '@/utils/storage'
import * as LoginApi from '@/api/login'
import * as userApi from '@/api/user.js'
import store from '..'

// 登陆成功后执行
const loginSuccess = (commit, token) => {
	// 过期时间30天
	const expiryTime = 30 * 86400
	// 保存tokne和userId到缓存

	// storage.set(USER_ID, userId, expiryTime)
	storage.set(ACCESS_TOKEN, token, expiryTime)
	// 记录到store全局变量
	commit('SET_TOKEN', token)
	// commit('SET_USER_ID', userId)
}

const loginSuccess_LOGIN = ({
	token
}) => {
	// 过期时间30天
	const expiryTime = 30 * 86400
	// 保存tokne和userId到缓存
	// storage.set(USER_ID, userId, expiryTime)
	storage.set(ACCESS_TOKEN, token, expiryTime)
}
const user = {
	state: {
		businessInfo: {
			businessId: ''
		},
		eliteInfo: null,
		user: null,
		permissions: null,
		roles: [],
		// 用户认证token
		token: '',
		//通讯录
		contacts: [],
		// 用户ID
		userId: null,
		/**
		 * certifiedState :是否实名
		 */
		my_userInfo: null,
	},

	mutations: {
		SET_CONTACTS: (state, value) => {
			state.contacts = value
		},
		SET_LOGIN_TOKEN: (state, value) => {
			state.token = value
			loginSuccess_LOGIN(value)
		},
		SET_TOKEN: (state, value) => {
			state.token = value
		},
		SET_USER_ID: (state, value) => {
			state.userId = value
		},
		SET_MY_userInfo: (state, value) => {
			let list = []
			state.my_userInfo = {
				...value.user
			}
			state.eliteInfo = {
				...value.eliteInfo
			}
			state.permissions = {
				...value.permissions
			}
			state.roles = list.concat(value.roles)
		}
	},

	actions: {
		setContacts({
			commit
		}, data) {
			commit('SET_CONTACTS', data)
		},
		userBaseInfo({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				if (this.state.user.token) {
					userApi.getInfo().then((result) => {
						const {
							eliteInfo,
							permissions,
							roles,
							user,
							businessInfo
						} = result
						// if (result.phone) {
						// 	let phonex1 = result.phone.substring(0, 3)
						// 	let phonex2 = result.phone.substring(result.phone.length - 4)
						// 	result.phonex = phonex1 + '****' + phonex2
						// }
						commit('SET_MY_userInfo', {
							eliteInfo,
							permissions,
							roles,
							user,
							businessInfo
						})
						commit('SET_USER_ID', user.userId)
						resolve(result)
					}).catch(reject)
				} else {
					reject
				}
			})
		},
		// 渠道页注册(渠道注册: 输入手机号和验证码和渠道码) 
		//只注册 不登录
		QD_register({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.QD_register({
						// account: data,
						// password: data
						...data
					})
					.then(response => {
						const result = response.result
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},
		//微信公众号登录
		wxH5Login({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.wxH5Login(data)
					.then(res => {
						const result = res.data
						if (res.user) {
							loginSuccess(commit, res.token)
						}
						resolve(res)
					})
					.catch(reject)
			})
		},
		// 用户登录(普通登录: 输入手机号和验证码)
		Login({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				// loginAccount moveLogin
				LoginApi.moveLogin({
						// account: data,
						// password: data
						...data
					})
					.then(response => {
						const result = response.token
						this.state.user.my_userInfo = {
							phone: '',
							certifiedState: 0
						}
						loginSuccess(commit, result)
						// console.log(this);
						// this.dispatch('userBaseInfo')
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 用户登录(普通登录: 输入手机号和验证码)
		register({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.registerAccount({
						...data
					})
					.then(response => {
						const result = response.data
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 退出登录
		Logout({
			commit
		}, data) {
			const store = this
			return new Promise((resolve, reject) => {
				let old_token = uni.getStorageSync(ACCESS_TOKEN) || this.getters.token
				if (!old_token) {
					return
				}
				// LoginApi.outLogin().then(res => {
				// 	if (old_token) {
				// 		// 删除缓存中的tokne和userId
				// 		storage.remove(USER_ID)
				// 		storage.remove(ACCESS_TOKEN)
				// 		// 记录到store全局变量
				// 		commit('SET_TOKEN', '')
				// 		commit('SET_USER_ID', null)
				// 		resolve()
				// 	}
				// })
				if (old_token) {
					// 删除缓存中的tokne和userId
					storage.remove(USER_ID)
					storage.remove(ACCESS_TOKEN)
					// 记录到store全局变量
					commit('SET_TOKEN', '')
					commit('SET_USER_ID', null)
					resolve()
				}

			})
		}

	}
}

export default user